﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WF_ExportTool.Configuration;
using WF_ExportTool.Configuration.Auto;
using WF_ExportTool.Configuration.DataV;
using WF_ExportTool.Configuration.Form;
using WF_ExportTool.Configuration.Grid;
using WF_ExportTool.Configuration.Tab;
using WF_ExportTool.Configuration.Tree;

namespace WF_ExportTool.Common
{
    public  class ModelConvertHelper
    {
        /// <summary>
        /// Grid_Main类dt转化为实体集合
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IList<Grid_Main> ConvertToModelByGridMain(DataTable dt)
        {
            // 定义集合    
            IList<Grid_Main> ts = new List<Grid_Main>();

            // 获得此模型的类型   
            Type type = typeof(Grid_Main);
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                Grid_Main t = new Grid_Main();
                // 获得此模型的公共属性   
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }

        /// <summary>
        /// Form_Main类转化为实体
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IList<Form_Main> ConvertToModelByFormMain(DataTable dt)
        {
            // 定义集合    
            IList<Form_Main> ts=new List<Form_Main>();

            // 获得此模型的类型   
            Type type = typeof(Form_Main);
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                Form_Main t = new Form_Main();
                // 获得此模型的公共属性   
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }


        /// <summary>
        /// CP_AutoNum类转化为实体
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IList<CP_AutoNum> ConvertToModelByAutoMain(DataTable dt)
        {
            // 定义集合    
            IList<CP_AutoNum> ts = new List<CP_AutoNum>();

            // 获得此模型的类型   
            Type type = typeof(CP_AutoNum);
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                CP_AutoNum t = new CP_AutoNum();
                // 获得此模型的公共属性   
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }

        /// <summary>
        /// Tab_Main类转化为实体
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IList<Tab_Main> ConvertToModelByTabMain(DataTable dt)
        {
            // 定义集合    
            IList<Tab_Main> ts = new List<Tab_Main>();

            // 获得此模型的类型   
            Type type = typeof(Tab_Main);
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                Tab_Main t = new Tab_Main();
                // 获得此模型的公共属性   
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }

        /// <summary>
        /// Tree_Main类转化为实体
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IList<Tree_Main> ConvertToModelByTreeMain(DataTable dt)
        {
            // 定义集合    
            IList<Tree_Main> ts = new List<Tree_Main>();

            // 获得此模型的类型   
            Type type = typeof(Tree_Main);
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                Tree_Main t = new Tree_Main();
                // 获得此模型的公共属性   
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }

        /// <summary>
        /// DataV_Main类转化为实体
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public IList<DataV_Main> ConvertToModelByDataVMain(DataTable dt)
        {
            // 定义集合    
            IList<DataV_Main> ts = new List<DataV_Main>();

            // 获得此模型的类型   
            Type type = typeof(DataV_Main);
            string tempName = "";
            foreach (DataRow dr in dt.Rows)
            {
                DataV_Main t = new DataV_Main();
                // 获得此模型的公共属性   
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
        /// <summary>
        /// 将DataTable转化为Dictionary
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="type">获得此模型的类型</param>
        /// <returns></returns>
        public IList<Dictionary<string, string>> ConvertToDicByDataTable(DataTable dt, BaseEntity t,Type type) {

           IList<Dictionary<string, string>> lisD = new List<Dictionary<string, string>>();  
            string tempName = "";
            foreach (DataRow dr in dt.Rows) {
                // 获得此模型的公共属性   
                PropertyInfo[] propertys = t.GetType().GetProperties();
                Dictionary<string, string> dic = new Dictionary<string, string>();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列    
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        dic.Add(pi.Name, value.ToString());

                    }

                }
                lisD.Add(dic);
            }
            return lisD;
        }
    }
}
